package com.zjzywl.iotSendthings.controller;

import java.io.IOException;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;

import com.zjzywl.iotSendthings.util.ImageTransferUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zjzywl.iotSendthings.entity.Verify;
import com.zjzywl.iotSendthings.repository.VerifyRepository;
import com.zjzywl.iotSendthings.util.WebResult;

/*
 * 人脸抓拍控制类
 */
@RestController
@RequestMapping(value = "/verify")
public class VerifyController extends BaseController {

    @Autowired
    private VerifyRepository repository;

    @PutMapping("/upload")
    public WebResult uploadImg(@RequestParam("picture") MultipartFile multipartFile, @RequestParam("uuid") String uuid,
                               @RequestParam(value = "result", required = false) int result) {
        if (multipartFile.isEmpty() || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
            return WebResult.FAIL("图片数据为空！");
        }
        String contentType = multipartFile.getContentType();
        if (!contentType.contains("")) {
            return WebResult.FAIL("文件类型为空！");
        }

        try {
            String file_name = ImageTransferUtil.saveImg(multipartFile, "verify", uuid);
            Verify v = new Verify();
            v.setUuid(uuid);
            v.setResult(result);
            v.setPicture("verify\\" + file_name);
            v.setCreatetime(new Date());
            repository.save(v);
            return WebResult.OK("接收图片成功！");
        } catch (IOException e) {
            return WebResult.FAIL(e.getMessage());
        }

    }

    @RequestMapping(value = "/getImg", method = RequestMethod.GET)
    public void doGetBackgroundImg(String uuid, HttpServletResponse response) throws Exception {
        Verify v = repository.findByUuid(uuid);
        String pictureFile = v.getPicture();
        ImageTransferUtil.getPictureByUrl(response, pictureFile);
    }

    @PostMapping(value = "/save")
    public WebResult save(@PathParam("uuid") String uuid, @PathParam("result") int result) {
        Verify v = new Verify();
        v.setUuid(uuid);
        v.setResult(result);
        v.setCreatetime(new Date());
        repository.save(v);
        return WebResult.OK();
    }

    @PostMapping(value = "/del")
    public WebResult del(@PathParam("uuid") String uuid) {
        try {
            repository.delectByUuid(uuid);
            return WebResult.OK();
        } catch (Exception e) {
            return WebResult.FAIL(e.toString());
        }

    }

    @GetMapping(value = "/data")
    public WebResult getPicture(@PathParam("uuid") String uuid) {
        Verify v = repository.findByUuid(uuid);
        if (v == null) {
            return WebResult.FAIL("找不到信息");
        } else {
            return WebResult.OK(v);
        }
    }


}
